const util = require("../../utils/util");

// pages/fabu/fabu.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: "", // 输入的文本内容
    fileList: []
  },
  // afterRead(event) {
  //   const { file } = event.detail;
  //   // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
  //   wx.uploadFile({
  //     url: 'https://example.weixin.qq.com/upload', // 仅为示例，非真实的接口地址
  //     filePath: file.url,
  //     name: 'file',
  //     formData: { user: 'test' },
  //     success(res) {
  //       // 上传完成需要更新 fileList
  //       const { fileList = [] } = this.data;
  //       fileList.push({ ...file, url: res.data });
  //       this.setData({ fileList });
  //     },
  //   });
  // },
    //new
  submitImage(e){
  const { rentImgIdList = [] } = this.data;
  const { fileList = [] } = this.data;
  //const { fileUrlList = [] } = this.data;
  //filePath是String类型只能单个上传url
  let that=this;
  fileList.forEach(function(e){  
    // var url=e.url;
    // fileUrlList.push(url);
 
    wx.uploadFile({
      url:app.data.SERVER_URLx+"/api/rent/upload/img",
      method:'POST',
      filePath: e.url,
      name: 'files',
      header: {"Authorization":"Bearer "+app.globalData.token},
      formData: { user: 'test' },
      success(res) {
        console.log(res.data);
        var result=JSON.parse(res.data);
        if( result.status==200){
          console.log( result.data[0].rentImgId+"成功上传:"+ result.data[0].imgAddr);
          rentImgIdList.push(result.data[0].rentImgId);
 
        }else{
          console.log(result.status+"上传失败:"+ result.msg);
        }
 
      }
      ,fail(res){
        console.log("上传fail");
      },complete(res){
        console.log("上传complete");
      }
      
    });
 
  });
  that.setData({
    rentImgIdList:rentImgIdList
  });
  console.log("submit image");
  console.log("submit image finish");
  },
  deleteImage(event){
  console.log(event);
  let that=this;
        const { fileList = [] } = that.data;
        fileList.splice(event.detail.index, 1)
        that.setData({ fileList });
  },
  afterRead(event) {    
    const { file } = event.detail;
    const that = this;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    const cos = util.getUploadToken();
    cos.postObject({
      Bucket: 'feng-1300275634',
      Region: 'ap-chengdu',
      Key: 'test/' + Math.random().toString(36).substr(2) + file.url.split('.').pop(),
      FilePath: file.url,
      onProgress: function (info) {
          console.log(JSON.stringify(info));
      }
  }, function (err, data) {
    if(err) {
      console.log("上传错误")
    } else {
      // 上传完成需要更新 fileList
      const { fileList = [] } = that.data;
      fileList.push({ ...file, url: `http://${data.Location}` });
      that.setData({ fileList });
    }
  });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取输入的内容
  getValue(e) {
    this.setData({
      content: e.detail.value
    })
  },
  // 保存攻略
  saveIdea() {
    if(!this.data.content) {
      return wx.showToast({
        title: '请输入内容',
      })
    }

    let params = {
      content: this.data.content
    }
    if(this.data.fileList.length) {
      params.pics = this.data.fileList.map(item => item.url).join(",")
    }
    wx.$http.post(wx.$api.fetchStrategyAdd, params).then(res => {
      wx.showToast({
        title: '发布成功',
      })
      wx.switchTab({
        url: '/pages/page1/page1',
      })
    })
    .catch(err => {
      wx.showToast({
        title: '发布失败',
      })
    })
  }
})